import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login/index.vue'
import Layout from '../views/Layout/index.vue'
import Home from '../views/Home/index.vue'
import Course from '../views/Course/index.vue'
import Personal from '../views/Personal/index.vue'
import MyPost from '../views/Post/MyPost.vue'
import Study from '../views/Study/index.vue'
import Coursedetail from '../views/Cdetail/index.vue'
import Teacher from '../views/Teacher/index.vue'
import Lesson from '../views/Lesson/index.vue'
import TeaInfo from '../views/Teainfo/index.vue'
import Addpost from '../views/Post/PostSubmitView.vue'

import Post from '../views/Post/PostView.vue'
import Postdetail from '../views/Post/PostDetails.vue'


import Lessondetail from '../views/Lesson/lessondetail.vue'

import { getToken } from '../utils/auth'
import { ElMessage, ElMessageBox } from 'element-plus'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'',
      name:'layout',
      component:Layout,
      children: [
        {
          path: '',
          name: 'home',
          component: Home
        },
        {
          path:'/course',
          name:'course',
          component:Course
  
        },
        {
          path:'/personal',
          name:'personal',
          component:Personal

        },
        {
          path:'/myPost',
          name:'myPost',
          component:MyPost
        },
        {
          path:'/courseintro/:id',
          component:Coursedetail
        },
        
        {
          path:'/teacher',
          component:Teacher

        },
        {
          path:'/addlesson/:id',
          component:Lesson
        },
        {
          path:'/study',
          component:Study
        },
        {
          path:'/teainfo/:id',
          component:TeaInfo
        },
        {
          path:'/addpost',
          component:Addpost
        },
       
        {
          path:'/lessondetail/:id',
          component:Lessondetail
        },
       {
        path:'/post',
        component:Post
       },
       {
        path:'/post/:id',
        component:Postdetail
       }
      ]
    },
 
    {
      path: '/login',
      name: 'login',
      component: Login
    }
    
  ]
})
router.beforeEach((to,from,next)=>{
  if(to.path === '/login'|| to.path=== '/'){
    next()
  }else{
    let token =getToken();
    if(!token){
      ElMessageBox.alert('您还没登录','提示',{
        confirmButtonText:'确定'
      })
      next('/login')
    }else{
      next();
    }
  }
})
export default router
